<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<%@ page trimDirectiveWhitespaces="true"%>
<%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="jakarta.tags.core" %>
<%@ taglib prefix="ujn" uri="http://ujn.edu.cn/common/"%>
<%
  String path = request.getContextPath();
  String basePath = request.getScheme() + "://" + request.getServerName()
          + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE HTML>
<html>
<head>
  <title>demo.jsp</title>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <!-- 引入css样式文件 -->
  <!-- Bootstrap Core CSS -->
  <link href="<%=basePath%>css/bootstrap.css" rel="stylesheet" />
  <!-- Bootstrap-grid CSS -->
  <link href="<%=basePath%>css/bootstrap-grid.css" rel="stylesheet" />
  <!-- Bootstrap-reboot CSS -->
  <link href="<%=basePath%>css/bootstrap-reboot.css" rel="stylesheet" />
</head>
<body>

<div>
  <nav class="navbar navbar-expand-lg navbar-dark " style="background-color:#81D8CF;;">
    <img src="imges/logo.jpg" class="rounded float-left" alt="Logo" height="45" width="45">
    <div class="container" style="">
      <a class="navbar-brand" href="#">停车场管理系统</a>
      <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarText"
              aria-controls="navbarText" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
      </button>
      <div class="collapse navbar-collapse" id="navbarText">
        <ul class="navbar-nav mr-auto">
          <li class="nav-item">
            <a class="nav-link " href="${pageContext.request.contextPath }/userself">个人信息</a>
          </li>
          <li class="nav-item">
            <a class="nav-link " href="${pageContext.request.contextPath }/bookself">车位预定</a>
          </li>
          <li class="nav-item">
            <a class="nav-link active" href="${pageContext.request.contextPath}/payments">全部订单</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="${pageContext.request.contextPath}/coupon">优惠券</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="${pageContext.request.contextPath }/rateself">收费标准</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="${pageContext.request.contextPath }/vehicleself">车辆信息</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="${pageContext.request.contextPath }/userequip">投诉反馈</a>
          </li>
        </ul>
        <span class="navbar-text">
          导航栏
        </span>
      </div>
    </div>
  </nav>
</div>
<%

  if (session.getAttribute("USER_SESSION")!=null) {
%>
<!--登录成功界面-->
<!--刷新账单按钮-->
<div class="container" style="margin-top: 20px;">
  <a href="#" class="btn btn-light" onclick="updatePayments()">更新账单</a>
</div>
<!--已支付订单的展示-->
<div class="container" style="padding-top: 20px; padding-bottom: 30px; text-align: center; background-color: rgba(255, 255, 255, 0.7);">
  <table class="table table-striped">
    <thead>
    <tr>
      <th>订单ID</th>
      <th>支付费用</th>
      <th>支付时间</th>
      <th>优惠券折扣</th>
      <th>操作</th>
    </tr>
    </thead>
    <tbody>
    <c:forEach items="${page.rows}" var="payments">
      <tr>
        <td>${payments.paymentId}</td>
        <td>${payments.cost}</td>
        <td>${payments.paymentTime}</td>
        <td>${payments.coupon.value}</td>
        <td>
          <form action="<%=basePath%>paying" method="get" style="display: inline;">
            <input type="hidden" name="payment_id" value="${payments.paymentId}">
            <input type="hidden" name="cost" value="${payments.cost}">
            <button class="btn btn-primary"
                    type="submit"
              ${payments.paymentTime != null ? 'disabled' : ''}>
              缴费
            </button>
          </form>
          <button class="btn btn-danger"
                  onclick="deletePayments(${payments.paymentId})"
            ${payments.paymentTime == null ? 'disabled' : ''}>
            删除
          </button>
        </td>
      </tr>
    </c:forEach>
    </tbody>
  </table>
  <!-- 分页组件 -->
  <div class="col-md-12 text-right">
    <ujn:page url="${pageContext.request.contextPath}/payments"/>
  </div>
</div>
<%
} else{
%>
<!--登陆失败界面-->
<div class="jumbotron jumbotron-fluid">
  <div class="container">
    <h1 class="display-4">欢迎您,登录后即可查看</h1>
    <p class="lead">
      <a href="${pageContext.request.contextPath}/login">Welcome, log in to view</a>
    </p>
  </div>
</div>
<%  }
%>

<!-- 引入js文件 -->
<script src="<%=basePath%>js/jquery-1.11.3.min.js"></script>
<!-- Bootstrap Core JavaScript -->
<script src="<%=basePath%>js/bootstrap.js"></script>
<!-- Bootstrap-bundle JavaScript -->
<script src="<%=basePath%>js/bootstrap.bundle.js"></script>
<!-- Metis Menu Plugin JavaScript -->
<script src="<%=basePath%>js/metisMenu.min.js"></script>
<!-- DataTables JavaScript -->
<script src="<%=basePath%>js/jquery.dataTables.min.js"></script>
<script src="<%=basePath%>js/dataTables.bootstrap.min.js"></script>
<!-- Custom Theme JavaScript -->
<script src="<%=basePath%>js/sb-admin-2.js"></script>

<script>
  //拉取更新订单
  function updatePayments() {
    if(confirm('确定要更新账单吗？')) {
      $.ajax({
        url:'<%=basePath%>payments',
        type:'PUT',
        success:function (data) {
          if(data == "OK") {
            alert("账单更新成功！");
            window.location.reload();
          } else {
            alert("暂无最新账单！");
            window.location.reload();
          }
        }
      });
    }
  }
  //删除已支付订单
  function deletePayments(id) {
    if (confirm('确实要删除该订单吗?')) {
      $.ajax({
        url: '<%=basePath%>payments/' + id,
        type: 'DELETE',
        success: function (data) {
          if (data == "OK") {
            alert("订单删除成功！");
            window.location.reload();
          } else {
            alert("订单删除失败！");
            window.location.reload();
          }
        }
      });
    }
  }
</script>
</body>
</html>
